// pages/personnel/user/setting-edit/index.js
import {
    editBindEmail,
    addBindEmail,
    sendSms,
    editRegisterPhone
} from "../../../service/Common";
import {editUser} from "../../../service/Student";
import Toast from "@vant/weapp/toast/toast";
Page({

    /**
     * 页面的初始数据
     */
    data: {
        name: "",
        email: "",
        phone: "",
        wx: "",
        smsButton: {
            loading: false,
            start: false
        },
        submitBtnLoading: false //提交按钮状态
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        this.data._options = options;
        this.setData({
            name: options.name,
            email: options.email || "",
            phone: options.phone || "",
            wx: options.wx || ""
        })
    },

    /*
    * 发送验证码监听
    * */
    onSendSms: async function (e) {
        try {
            let phone = e.detail.phone;
            this.setData({"smsButton.loading": true});
            await sendSms(phone);
            this.setData({"smsButton.start": true})
        } catch (e) {
            console.error(e)
        } finally {
            this.setData({"smsButton.loading": false});
        }
    },

    /*
    * 监听点击按钮保存
    * */
    onSave: async function (e) {
        try {
            const that = this;
            let name = this.data.name;
            let emailId = this.data._options.emailId;
            let registerId = this.data._options.registerId;
            let submitBtnLoading = this.data.submitBtnLoading;
            let email = e.detail.email;
            let phone = e.detail.phone;
            let yzm = e.detail.sms;
            let vx = e.detail.wx;
            if (submitBtnLoading) return false; //防重复
            this.setData({submitBtnLoading: true});
            if (name === "email") {
                if (emailId) {
                    await editBindEmail({
                        id: emailId,
                        email: email
                    })
                } else {
                    emailId = await addBindEmail(email)
                }
            } else if (name === "phone") {
                await editRegisterPhone({
                    id: registerId,
                    phone,
                    yzm
                })
            } else if (name === "wx") {
                await editUser({
                    id: registerId,
                    vx: vx
                })
            }
            Toast.success("保存成功");
            setTimeout(function () {
                const eventChannel = that.getOpenerEventChannel()
                eventChannel.emit("acceptDataFromOpenedPage", {
                    name,
                    email,
                    emailId,
                    phone,
                    wx: vx
                });
                wx.navigateBack()
            }, 1000)
        } catch (e) {
            Toast.fail("修改失败");
            console.error(e)
        } finally {
            this.setData({submitBtnLoading: false})
        }
    }
})